'use strict';


swal.setDefaults({confirmButtonText: lang.ok});

var timer_value = 2000;

function checkSlug(s) {
    var patrn = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
    if (!patrn.exec(s)) return false
    return true
}

/**
 * adminApp Module
 *
 * Description
 */

var adminApp = angular.module('app',
    [
        'ngRoute',
        'ngTable',
        'ngResource',
        'oitozero.ngSweetAlert',
        'ngSanitize',
        'ui.select',
        'ngDialog',
        'ngMask',
        'angular-ladda',
        'ngCkeditor',
        'ngFileUpload'
    ])
    .constant('DASHBOARD', Otk.DASHBOARD)
    .constant('DASHBOARD_API', Otk.DASHBOARD_API)
    .constant('TEMPLATE', '/' + Otk.TEMPLETE)
    .config(function (uiSelectConfig) {
        uiSelectConfig.theme = 'bootstrap';
    })
    .config(['$httpProvider', function ($httpProvider) {
        $httpProvider.interceptors.push('errorInterceptor');
    }])
    .run(function ($rootScope, $window, $route) {
        $rootScope._ = $window._;
        $rootScope.getLabelClass = function (index) {
            if(index % 5 === 0){
                return 'label-primary';
            }
            if(index % 5 === 1){
                return 'label-danger';
            }
            if(index % 5 === 2){
                return 'label-success';
            }
            if(index % 5 === 3){
                return 'label-info';
            }
            if(index % 5 === 4){
                return 'label-warning';
            }
        };
        $rootScope.currentUser = JSON.parse(localStorage.getItem('currentUser'));
        $rootScope.editorOptions = {
            toolbar: 'full',
            toolbar_full: [
                {
                    name: 'basicstyles',
                    items: ['Bold', 'Italic', 'Strike', 'Underline']
                },
                {name: 'paragraph', items: ['BulletedList', 'NumberedList', 'Blockquote', 'BidiLtr', 'BidiRtl']},
                {name: 'editing', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock']},
                {name: 'links', items: ['Link', 'Unlink', 'Anchor']},
                {name: 'tools', items: ['SpellChecker', 'Maximize']},
                {name: 'clipboard', items: ['Undo', 'Redo']},
                {
                    name: 'styles',
                    items: ['Format', 'FontSize', 'TextColor', 'BGColor', 'PasteText', 'PasteFromWord', 'RemoveFormat']
                },
                {name: 'insert', items: ['Image', 'Table', 'SpecialChar', 'MediaEmbed']}, '/',
            ]
        };
    })
    .run([
        "$location",
        "$route",
        "$rootScope",
        function ($location, $route, $rootScope) {
            $location.searchReplace = function () {
                var lastRoute = $route.current;
                var un = $rootScope.$on("$locationChangeSuccess", function () {
                    $route.current = lastRoute;
                    un();
                });
                return $location.search.apply($location, arguments);
            };
        }])
    .filter('propsFilter', function() {
        return function(items, props) {
            var out = [];

            if (angular.isArray(items)) {
                var keys = Object.keys(props);

                items.forEach(function(item) {
                    var itemMatches = false;

                    for (var i = 0; i < keys.length; i++) {
                        var prop = keys[i];
                        var text = props[prop].toLowerCase();
                        if (item[prop].toString().toLowerCase().indexOf(text) !== -1) {
                            itemMatches = true;
                            break;
                        }
                    }

                    if (itemMatches) {
                        out.push(item);
                    }
                });
            } else {
                // Let the output be the input untouched
                out = items;
            }

            return out;
        };
    });